//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.PtChangeShop;
import com.aidong.model.wills.PtContract;
import java.util.ArrayList;
import java.util.Date;
import org.springframework.stereotype.Repository;

@Repository
public class PtChangeShopDao extends BaseDao<PtChangeShop, Integer> {
	public PtChangeShopDao() {
	}

	public void queryPageList(Pagination<PtChangeShop> pg, ConditionMap cd) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select b.autoId,b.shopCode,b.contractCode,b.memberCode, ptCode,coachCode, newCoachCode, m.chName memberName, b.createTime\nfrom PtChangeShop b inner join MemberInfo m on b.memberCode=m.memberCode\n" + (sqlw.length() > 0?"where " + sqlw:"") + " order by b.createTime desc";
		this.queryPageListBySql(PtChangeShop.class, pg, sql, params.toArray());
	}

	public void addChangeShop(PtChangeShop changeShop, PtContract newContract) {
		String shopCode = newContract.getShopCode();
		String sql = "select max(contractCode) from PtContract where shopCode=:p1 and contractCode like :p2";
		String maxCode = (String)this.getBySql(String.class, sql, new Object[]{shopCode, shopCode + "%"});
		int maxId = 0;
		if(maxCode != null && maxCode.length() > shopCode.length()) {
			try {
				maxId = Integer.parseInt(maxCode.substring(shopCode.length()));
			} catch (Exception var10) {
				;
			}
		}

		String contractCode = "000000" + (maxId + 1);
		contractCode = shopCode + contractCode.substring(contractCode.length() - 6);
		changeShop.setNewContractCode(contractCode);
		this.add(changeShop);
		sql = "update PtContract set outQuantity=:p1, outAmount=:p2, outType=:p3, outTime=:p4, outUser=:p5, updateTime=:p6 where contractCode=:p7";
		this.updateBySql(sql, new Object[]{changeShop.getOutQuantity(), changeShop.getOutAmount(), "2", new Date(), changeShop.getCreateUser(), new Date(), changeShop.getContractCode()});
		newContract.setContractCode(contractCode);
		this.getSession().save(newContract);
		String memberName = (String)this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{newContract.getMemberCode()});
		ContractSign sign = new ContractSign();
		sign.setShopCode(newContract.getShopCode());
		sign.setContractCode(newContract.getContractCode());
		sign.setContractType("02");
		sign.setSaleId(newContract.getSaleId());
		sign.setMemberCode(newContract.getMemberCode());
		sign.setMemberName(memberName == null?"":memberName);
		sign.setStatus(Integer.valueOf(1));
		sign.setSalesCode(newContract.getSalesCode());
		sign.setSalesCode2(newContract.getSalesCode2());
		sign.setCreateUser(newContract.getCreateUser());
		sign.setCreateTime(new Date());
		sign.setUpdateTime(new Date());
		this.getSession().save(sign);
	}
}
